package cn.ztydata.teachingsystem.heaven.dao;

import cn.ztydata.teachingsystem.heaven.entity.exercise.BlankSolution;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * 习题填空答案Dao
 * Created by wyh on 2015/2/6.
 */
@Repository
public class BlankSolutionDao extends BaseDao {

    /**
     * 新增填空答案（批量）
     * @param solutions 填空答案集合
     * @return int[]
     *
     * @author wyh
     * @since  2015-2-5
     */
    public int[] saveBlankSolutions(List<BlankSolution> solutions){
        String sql = "INSERT INTO t_blank_solution(iBlankId,sTextSolution,sHtmlSolution) VALUES(:blankId,:textSolution,:htmlSolution)";

        SqlParameterSource[] batch = SqlParameterSourceUtils.createBatch(solutions.toArray());

        log.debug("批量新增填空答案");
        return namedParameterJdbcTemplate.batchUpdate(sql,batch);
    }

    /**
     * 获取填空答案
     * @param blankId
     * @return
     * @author lyj
     * @since 2015-2-9
     */
    public List<BlankSolution> getBlankSolutions(final long blankId){
        String sql = "SELECT id,sTextSolution,sHtmlSolution FROM t_blank_solution WHERE iBlankId = :iBlankId";

        MapSqlParameterSource param = new MapSqlParameterSource().addValue("iBlankId", blankId);

        log.debug("获取填空答案");
        return namedParameterJdbcTemplate.query(sql, param, new RowMapper<BlankSolution>() {
            @Override
            public BlankSolution mapRow(ResultSet resultSet, int i) throws SQLException {
                BlankSolution blankSolution = new BlankSolution();

                blankSolution.setId(resultSet.getLong("id"));
                blankSolution.setBlankId(blankId);
                blankSolution.setTextSolution(resultSet.getString("sTextSolution"));
                blankSolution.setHtmlSolution(resultSet.getString("sHtmlSolution"));

                return blankSolution;
            }
        });
    }
}
